﻿if(!window.Element) 
{
	// 使IE支援Element
    Element = function(){};

    var __createElement = document.createElement;
    document.createElement = function(tagName)
    {
        var element = __createElement(tagName);
        if (element == null) {return null;}
        for(var key in Element.prototype)
                element[key] = Element.prototype[key];
        return element;
    }

    var __getElementById = document.getElementById;
    document.getElementById = function(id)
    {
        var element = __getElementById(id);
        if (element == null) {return null;}
        for(var key in Element.prototype)
                element[key] = Element.prototype[key];
        return element;
    }
	
	var __getElementsByTagName = document.getElementsByTagName;
    document.getElementsByTagName = function(id)
    {
        var element = __getElementsByTagName(id);
        if (element == null) {return null;}
		for(i = 0; i < element.length; i++)
        {
			for(var key in Element.prototype)
					element[i][key] = Element.prototype[key];
		}
        return element;
    }
	
	var __getElementsByName = document.getElementsByName;
    document.getElementsByName = function(id)
    {
        var element = __getElementsByName(id);
        if (element == null) {return null;}
		for(i = 0; i < element.length; i++)
        {
			for(var key in Element.prototype)
					element[i][key] = Element.prototype[key];
		}
        return element;
    }
	
}

// 验证整数
Element.prototype.isNum = function(){
	if(isText(this)) return undefined;
	var value = String(parseInt(this.value));
	if(value.length != this.value.length || value == "NaN")
		return false;
	return true;
};

// 验证浮点数
Element.prototype.isFloat = function(){
	if(isText(this)) return undefined;
	var value = String(parseFloat(this.value));
	if(value.length != this.value.length || value == "NaN")
		return false;
	return true;
};

// 验证日期
Element.prototype.isDate = function(){
	if(isText(this)) return undefined;
	var regex = new RegExp("^(?:(?:([0-9]{4}(-|\/)(?:(?:0?[1,3-9]|1[0-2])(-|\/)(?:29|30)|((?:0?[13578]|1[02])(-|\/)31)))|([0-9]{4}(-|\/)(?:0?[1-9]|1[0-2])(-|\/)(?:0?[1-9]|1\\d|2[0-8]))|(((?:(\\d\\d(?:0[48]|[2468][048]|[13579][26]))|(?:0[48]00|[2468][048]00|[13579][26]00))(-|\/)0?2(-|\/)29))))$"); 
	var dateValue = this.value; 
	if (!regex.test(dateValue))  
		return false; 
	return true; 
} 

// 验证空值
Element.prototype.isEmpty = function(){
	if(!this.type || (this.type != "text" && this.type != "password" && this.type != "hidden" )) return undefined;
	if (this.value.length > 0)  
		return false; 
	return true; 
} 

// 自定义正则验证
Element.prototype.isRegEx = function(reg){
	if(isText(this)) return undefined;
	
	if(reg.source){
		if(this.value.search(reg) < 0)
			return false;
	}
	else
	{
		var regex;
		regs = reg.split("/");
		if(regs.length > 0 && regs[0] == "" && regs[regs.length - 1].length == 1)
		{
			pattern = regs[1];
			attributes = regs[2];
			regex = new RegExp(pattern, attributes); 
		}
		else 
		{
			regex = new RegExp(reg); 
		}
		var dateValue = this.value; 
		if (!regex.test(dateValue))  
			return false; 
	}
	return true; 
}
 
// 验证空值
Element.prototype.isEmpty = function(){
	if(isText(this)) return undefined;
	if (this.value.length > 0)  
		return false; 
	return true; 
} 

// 验证集合
Element.prototype.is = function(type, param){
	if(isText(this)) return undefined;
	switch(type)
	{
		case 'int'	:	return this.isNum();
		case 'float':	return this.isFloat();
		case 'date'	:	return this.isDate();
		case 'empty':	return this.isEmpty();
		case 'regex':	return this.isRegEx(param);
		
		case 1		:	return this.isNum();
		case 2		:	return this.isFloat();
		case 3		:	return this.isDate();
		case 4		:	return this.isEmpty();
		case 5		:	return this.isRegEx(param);
	}
	return undefined;
} 

// 获取长度(中文算两个字符)
Element.prototype.getLength = function(){
	if(isText(this)) return undefined;
	return this.value.replace(/[^\x00-\xff]/g,"**").length; 
} 

// 获取长度(中文算一个字符)
Element.prototype.getnLength = function(){
	if(isText(this)) return undefined;
	return this.value.replace(/[^\x00-\xff]/g,"*").length; 
} 

// 获取Select控件选择项索引，值和文本
Element.prototype.getSelected = function(){
	if(!this.type || this.type.indexOf("select") < 0) return undefined;
	if(this.type =="select-one")
	{
		var sel;
		sel.index = this.selectedIndex;  
		sel.value = this.options[this.selectedIndex].value;  
		sel.text  = this.options[this.selectedIndex].value;  
		return sel; 
	}
	else if(this.type =="select-multiple")
	{
		var sels = [];
		for(i = 0; i < sel.length; i++)
		{
			if(this.options[i].selected)
			{
				l = sel.length;
				sel[l].index = i;  
				sel[l].value = this.options[i].value;  
				sel[l].text  = this.options[i].value;  
			}
		}
		return sels;
	}
	return undefined;
} 

isText = function(obj){
	if(!this.type || (this.type != "text" 
	&& this.type != "password" 
	&& this.type != "hidden" 
	&& this.type != "email" 
	&& this.type != "number" 
	&& this.type != "tel" )) return false;
	return true;
}

/*----------------------------------------------------------|
 | Ajax请求                                                 |
 | url : 请求地址                                           |
 | fn  : 接收请求函数，自定义。                             |
 | post: 发送post请求时，发送的数据。                       |
 |       使用get请求时，此项不填。                          |
 |----------------------------------------------------------*/
Ajax = function(url, fn){
    var xmlHttpReq = null;
    if (window.XMLHttpRequest){
        xmlHttpReq = new XMLHttpRequest();
    }
    else if (window.ActiveXObject){
		// IE5 IE6是通过这种方式
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(xmlHttpReq != null){
		xmlHttpReq.onreadystatechange = RequestCallBack; 
        xmlHttpReq.open("get", url, true);
        xmlHttpReq.send(null);
    }
	
	function RequestCallBack(){
        if(xmlHttpReq.readyState == 4)
        {
			if(fn)
				return fn(xmlHttpReq.responseText);
			else
				return undefined;
		}
	}
	return true;
}

if(!document.getElementsByClassName){
    document.getElementsByClassName = function(className, element){
        var children = (element || document).getElementsByTagName('*');
        var elements = new Array();
        for (var i=0; i < children.length; i++){
            var child = children[i];
            var classNames = child.className.split(' ');
            for (var j=0; j<classNames.length; j++){
                if (classNames[j] == className){ 
                    elements.push(child);
                    break;
                }
            }
        } 
        return elements;
    };
}







